package approve.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import approve.action.App_PendingDocument;
import approve.action.App_ReferenceDoc_Waiting;
import approve.action.My_App_SendingDocument;
import approve.action.R;
import approve.model.Approvedoc;

public class ApproveDocumentGeneralNotShowAdapter extends BaseAdapter {
	private Context context;
	private List<Approvedoc> lstApp;
	int screenwidth;
	String lable;
	LinearLayout flag = null;
	String flagForm="";
	ViewHolder holder;
	public ApproveDocumentGeneralNotShowAdapter(Context context,List<Approvedoc> lstApp,int screenwidth,String lable){
		this.context = context;
		this.lstApp = lstApp;
		this.screenwidth= screenwidth;
		this.lable=lable;
	}
	public ApproveDocumentGeneralNotShowAdapter(App_PendingDocument context,List<Approvedoc> lstApp,int screenwidth,String lable,String flagForm){
		this.context = context;
		this.lstApp = lstApp;
		this.screenwidth= screenwidth;
		this.lable=lable;
		this.flagForm = flagForm;
	}

	public ApproveDocumentGeneralNotShowAdapter(App_ReferenceDoc_Waiting context,List<Approvedoc> lstApp,int screenwidth,String lable,String flagForm){
		this.context = context;
		this.lstApp = lstApp;
		this.screenwidth= screenwidth;
		this.lable=lable;
		this.flagForm = flagForm;
	}

	
	public ApproveDocumentGeneralNotShowAdapter(My_App_SendingDocument context,List<Approvedoc> lstApp,int screenwidth,String lable,String flagForm){
		this.context = context;
		this.lstApp = lstApp;
		this.screenwidth= screenwidth;
		this.lable=lable;
		this.flagForm = flagForm;
	}
	
	
	
	@Override
	public int getCount() {
		return lstApp.size();
	}

	@Override
	public Object getItem(int arg0) {
		return lstApp.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertview, ViewGroup parent) {
		final Approvedoc doc = lstApp.get(position);
		if(convertview==null){
			holder = new ViewHolder();
			LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertview=inflater.inflate(R.layout.adapter_list_approval, parent,false);
			
			holder.imageView = (ImageView) convertview.findViewById(R.id.logo);
			holder.imageView.setImageResource(R.drawable.text);
			
			holder.textView = (TextView) convertview.findViewById(R.id.labelListApp);
			holder.textViewNew = (TextView)convertview.findViewById(R.id.textViewNew);
			holder.textdate = (TextView) convertview.findViewById(R.id.labeldate);
			holder.lstAppr = (LinearLayout)convertview.findViewById(R.id.lstAppr);
			convertview.setBackgroundResource(R.drawable.list_selector);
			convertview.setTag(holder);
		}
		else{
			holder = (ViewHolder)convertview.getTag();
		}

		holder.textViewNew.setVisibility(View.GONE);
		holder.textView.setText(doc.getTitle());	

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		holder.textdate.setText(lable+": "+df.format(doc.getTimecreate()));
		
		holder.lstAppr.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(flagForm.equals("pendingdoc")){
					((App_PendingDocument)context).getAlertwaiting2().show();
					try{
						new Thread(){
							public void run() {
								((App_PendingDocument)context).loadAllPending(doc);
								((App_PendingDocument)context).getUiHandler2().post( new Runnable(){
									@Override
									public void run() {
										if(((App_PendingDocument)context).isUpdateRequired2()){
										}else{
											((App_PendingDocument)context).getAlertwaiting2().cancel();
										}
									}
								} );
							}
						}.start();
					}catch (Exception e) {}
					
				}else if(flagForm.equals("referdocwaiting")){
					((App_ReferenceDoc_Waiting)context).getAlertwaiting2().show();
					try{
						new Thread(){
							public void run() {
								((App_ReferenceDoc_Waiting)context).loadAllReferWaiting(doc);
								((App_ReferenceDoc_Waiting)context).getUiHandler2().post( new Runnable(){
									@Override
									public void run() {
										if(((App_ReferenceDoc_Waiting)context).isUpdateRequired2()){
										}else{
											((App_ReferenceDoc_Waiting)context).getAlertwaiting2().cancel();
										}
									}
								} );
							}
						}.start();
					}catch (Exception e) {}
					
				}else if(flagForm.equals("mysendingdoc")){
					
					((My_App_SendingDocument)context).getAlertwaiting2().show();
					try{
						new Thread(){
							public void run() {
								((My_App_SendingDocument)context).loadAllMySendingDoc(doc);
								((My_App_SendingDocument)context).getUiHandler2().post( new Runnable(){
									@Override
									public void run() {
										if(((My_App_SendingDocument)context).isUpdateRequired2()){
										}else{
											((My_App_SendingDocument)context).getAlertwaiting2().cancel();
										}
									}
								} );
							}
						}.start();
					}catch (Exception e) {}
				}
			}
		});
		
		return convertview;
	}
	
	public static class ViewHolder{
		ImageView imageView;
		TextView textView,textViewNew,textdate;
		LinearLayout lstAppr;
	}
}
